import random
import time
from database import Mysql
import conf
from datetime import datetime, timedelta


def update_system(db):

    target_date = datetime(2025, 6, 16).timestamp() * 1000  # 转为毫秒时间戳

    target_date_1 = datetime(2025, 6, 17).timestamp() * 1000  # 转为毫秒时间戳

    # 1、更新人员、角色、部门、企业部门数据字典、企业员工档案管理的创建时间、最后更新时间
    system_tables = [
        "sys_user",
        "sys_role",
        "sys_dept",
        "base_dept",
        "base_user",
    ]

    for table1 in system_tables:
        sql1 = f"""
                UPDATE {table1}
                SET create_time = {target_date} + 
                    (create_time % (24 * 60 * 60 * 1000)),  -- 保留原时间的毫秒部分
                    last_update_time = {target_date} + 
                    (last_update_time % (24 * 60 * 60 * 1000))
            """
        count1 = db.update(sql1)
        print(f"SQL1 更新{table1}时间: 影响 {count1} 行")

    # 2、更新仓库、物料、物料分类的创建时间、最后更新时间
    warehouse_tables = [
        "wms_warehouse",
        "base_goods",
        "base_goods_type",
    ]

    for table2 in warehouse_tables:
        sql2 = f"""
                UPDATE {table2}
                SET create_time = {target_date_1} + 
                    (create_time % (24 * 60 * 60 * 1000)),
                    last_update_time = {target_date_1} + 
                    (last_update_time % (24 * 60 * 60 * 1000))
            """
        count2 = db.update(sql2)
        print(f"SQL2 更新{table2}时间: 影响 {count2} 行")

    # 3、更新供应商信息、客户信息的创建时间、最后更新时间
    partner_tables = [
        "base_customer",
        "base_supplier_info",
    ]

    for table3 in partner_tables:
        sql3 = f"""
                UPDATE {table3}
                SET create_time = {target_date_1} + 
                    (create_time % (24 * 60 * 60 * 1000)),
                    last_update_time = {target_date_1} + 
                    (last_update_time % (24 * 60 * 60 * 1000))
            """
        count3 = db.update(sql3)
        print(f"SQL3 更新{table3}时间: 影响 {count3} 行")

    db.commit()
